#!/bin/bash
#提交者：王洋洋
#设计思路：1）首先设计一个judge函数，判断输入的参数是否正确 2）如果不是再设计一个函数menu输出正确版本号帮助下载 3）在下载时写一个exchange函数将输入的1.1  1.2等数字再次替换为之前的变量便于调用judge函数时比较 4）设计一个存入路径的函数CodePath来下载存放对应的版本 4）设计一个download函数来进行下载输出
#代码出错时直接跳出脚本
set -e
  version=$1
  version1=""
  OpenHarmony_master=master
  OpenHarmony_3_2_beta2=OpenHarmony-3.2-beta2
  OpenHarmony_3_2_beta2_merge=OpenHarmony-3.2-beta2-merge
  OpenHarmony_3_2_release=OpenHarmony-3.2-release
  html=https://gitee.com/openharmony/manifest.git
  m=""
  init_cmd=""
  lfs_cmd="repo forall -c 'git lfs pull'"
  sync_cmd="repo sync -c"
#创建路径，如果不存在就直接创建，存在则创建_temp或者删除重建
function CodePath() 
{
  if [ ! -e "$version" ]; then
    mkdir "$version"
    cd "$version"
  elif [ ! -e "$version"_temp ]; then
    mkdir "$version"_temp
    cd "$version"_temp
  else
    rm -rf "$version"_temp
    mkdir "$version"_temp
    cd "$version"_temp
  fi
}
#下载成功时打印出的信息
function download()
{
  CodePath
  echo "you have been download $version code"
  echo "url       : $html "
  echo "branch    : $version"
  echo "xml_name  : ${init_cmd}"
  echo "code_path : $(pwd) "
  echo "init   cmd: repo init -u $html -b $version1 $m  $init_cmd --no-repo-verify"
  echo "sync   cmd: $sync_cmd"
  echo "lfs    cmd: $lfs_cmd"
  echo "set_br cmd: repo start $version2 --all"
  exit 0
}
#当输入1.1 1.2等等版本号进行下载时将它再次转为之前的版本名方便下面的judge函数比较
function exchange()
{
if [ $version = "1.1" ];then
  version=$OpenHarmony_master
elif [ $version = "1.2" ];then
  version=$OpenHarmony_3_2_beta2
elif [ $version = "1.3" ];then
  version=$OpenHarmony_3_2_beta2_merge
else
  version=$OpenHarmony_3_2_release
fi
}
#当输入为空时输出以下信息来帮助下载者下载版本
function menu()
{
  echo "***********************************************"
  echo "* Welcome to download OpenHarmony Code 	      *"
  echo "* Please Choice OpenHarmony SDK:	      *"
  echo "* OpenHarmony Version             	      *"
  echo "* OpenHarmony master        	prese 1.1     *"
  echo "* OpenHarmony-3.2-beta2      	press 1.2     *"
  echo "* OpenHarmony-3.2-beta2-merge   press 1.3     *"
  echo "* OpenHarmony-3.2-release  	press 1.4     *"
  echo "***********************************************"
#输入一个版本号 
  read -p "input the version above:" num
  version=$num
  exchange
  judge

  exit 0

}
#通过judge函数判断所输入的版本号是否正确
function judge()
{
if [ "$version"x = "$OpenHarmony_master"x ];then
  version1="master"
  version2="master"
elif [ "$version"x = "$OpenHarmony_3_2_beta2"x ];then
  init_cmd=""
  version1="OpenHarmony-3.2-Beta2"
  version2="OpenHarmony-3.2-Beta2"
elif [ "$version"x = "$OpenHarmony_3_2_beta2_merge"x ];then
  init_cmd="thead_develop.xml"
  m="- m"
  version1="OpenHarmony-3.2-beta2-Merge"
  version2="OpenHarmony-3.2-beta2"
elif [ "$version"x = "$OpenHarmony_3_2_release"x ];then
  init_cmd="default.xml"
  m="- m"
  version1="OpenHarmony-3.2-Release"
  version2="OpenHarmony-3.2-Release"
elif [ -z $version ];then
  menu
else
  echo "product is not in product list, please recheck!!!"
  menu
  exit 0
fi
  download 
  exit 0
}
#主函数进行最终调用
function main()
{
  judge
}
main
